const DEFAULT_COLORS = ["#0e77ca", "#0cf", "#498b7f", "#2a6496", "#108ee9", "#439a9e", '#80FFA5', '#00DDFF', '#37A2FF',
    '#FF0087', '#FFBF00', "#88cc81", '#fda67e', '#81cacc', '#cca8ba', "#88cc81", "#82a0c5", '#fddb7e',
    '#735ba1', '#bda29a', '#6e7074', '#546570', '#c4ccd3'
]
export const chartOptions = [
    {
        value: 'scatter',
        key: 'scatter',
        label: '散点图',
        options: [
            {
                "label": "X轴",
                "key": 'x'
            },
            {
                "label": "Y轴",
                "key": 'y'
            },
        ]
    },
    {
        value: 'line',
        key: 'line',
        label: '折线图',
        options: [
            {
                "label": "X轴",
                "key": 'x'
            },
            {
                "label": "Y轴",
                "key": 'y'
            },
        ]
    },
    {
        value: 'pie',
        key: 'pie',
        label: '饼图',
        options: [
            {
                "label": "标签",
                "key": 'x',
            },
            {
                "label": "值",
                "key": 'y',
            },
        ]
    },
    {
        value: 'barVertical',
        key: 'bar-vertical',
        label: '柱状图-竖直',
        options: [
            {
                "label": "X轴",
                "key": 'x'
            },
            {
                "label": "Y轴",
                "key": 'y'
            },
        ]
    },
    {
        value: 'barHorizontal',
        key: 'bar-horizontal',
        label: '柱状图-水平',
        options: [
            {
                "label": "X轴",
                "key": 'x'
            },
            {
                "label": "Y轴",
                "key": 'y'
            },
        ]
    },
    // {
    //     value: 'timelinesVertical',
    //     key: 'timelines-vertical',
    //     label: '时间线-竖直',
    //     options: [
    //         {
    //             "label": "时间值"
    //         },
    //         {
    //             "label": "X轴",
    //             "key": 'x'
    //         },
    //         {
    //             "label": "Y轴",
    //             "key": 'y'
    //         },
    //     ]
    // },
    // {
    //     value: 'timelinesHorizontal',
    //     key: 'timelines-horizontal',
    //     label: '时间线-水平',
    //     options: [
    //         {
    //             "label": "时间值"
    //         },
    //         {
    //             "label": "X轴",
    //             "key": 'x'
    //         },
    //         {
    //             "label": "Y轴",
    //             "key": 'y'
    //         },
    //     ]
    // },
    {
        value: 'wordscloud',
        key: 'wordscloud',
        label: '词云',
        options: [
            {
                "label": "标签",
                "key": 'x',
            },
            {
                "label": "值",
                "key": 'y',
            },
        ]
    },
]
const renderOptions = {
    "scatter": {
        tooltip: {
            trigger: 'axis'
        },
        xAxis: {
            type: 'category',
        },
        yAxis: {
            type: 'value'
        },
        series: [
            {
                symbolSize: 20,
                data: [],
                type: 'scatter'
            }
        ]
    },
    'line': {
        tooltip: {
            trigger: 'axis'
        },
        xAxis: {
            type: 'category',
            data: []
        },
        yAxis: {
            type: 'value'
        },
        series: [
            {
                data: [],
                type: 'line',
                smooth: true
            }
        ]
    },
    "pie": {
        tooltip: {
            trigger: 'item'
        },
        legend: {
            orient: 'vertical',
            left: 'left'
        },
        series: [
            {
                type: 'pie',
                radius: '50%',
                data: [],
                textStyle: {},
                emphasis: {
                    itemStyle: {
                        shadowBlur: 10,
                        shadowOffsetX: 0,
                        shadowColor: 'rgba(0, 0, 0, 0.5)'
                    }
                }
            }
        ]
    },
    'barHorizontal': {
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'shadow'
            }
        },
        legend: {},
        grid: {
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
        },
        xAxis: {
            type: 'value',
        },
        yAxis: {
            type: 'category',
            data: []
        },
        series: [
            {
                type: 'bar',
                data: []
            }
        ]
    },
    'barVertical': {
        tooltip: {
            trigger: 'axis',
            axisPointer: {
                type: 'shadow'
            }
        },
        xAxis: {
            type: 'category',
            data: []
        },
        yAxis: {
            type: 'value'
        },
        series: [
            {
                data: [],
                type: 'bar'
            }
        ]
    },
    'wordscloud': {
        grid: {
            left: '1%',
            right: '2%',
            bottom: '2%',
            containLabel: true
        },
        series: [
            {
                type: 'wordCloud',
                // shape: 'circle',
                shape: 'ellipse',
                left: 'center',
                top: 'center',
                width: '90%',
                height: '90%',
                sizeRange: [12, 60],
                rotationRange: [0, 1],
                rotationStep: 45,
                gridSize: 8,
                drawOutOfBound: false,
                textStyle: {},
                emphasis: {
                    shadowBlur: 10,
                    shadowColor: '#333'
                },
                data: []
            }
        ]
    }
}

export function generateChartRenderOption(data, settings, showLabel, colors, colorFlag = true) {
    const type = settings.value
    let option = renderOptions[type];
    if (!data || data.length === 0 || !option) {
        return;
    }
    let xColumn = settings.options.find(o => o.key === 'x'),
        yColumn = settings.options.find(o => o.key === 'y'),
        tmp1 = [], tmp2 = [];
    if (xColumn) {
        let columnName = xColumn.value
        data.forEach((d, index) => {
            tmp1[index] = d[columnName] || 0
        })
        if (option.xAxis) {
            option.xAxis['name'] = columnName
        }
    }
    if (yColumn) {
        let columnName = yColumn.value
        data.forEach((d, index) => {
            tmp2[index] = d[columnName] || 0
        })
        if(option.yAxis){
            option.yAxis['name'] = columnName
        }
    }
    if (type === 'scatter') {
        let tmp = []
        tmp1.forEach((t, index) => {
            tmp[index] = [t, tmp2[index]]
        })
        option.series[0].data = tmp
    } else if (type === 'line') {
        option.xAxis.data = tmp1
        option.series[0].data = tmp2
    } else if (type === 'pie' || type === 'wordscloud') {
        let tmp = []
        tmp1.forEach((t, index) => {
            tmp[index] = {
                name: t,
                value: tmp2[index] || 0
            }
        })
        option.series[0].data = tmp
        option.series[0].textStyle.color = (e) => {
            let weight = e.data.value;
            // Random color
            if (colorFlag) {
                return 'rgb(' + [
                    Math.round(Math.random() * 160),
                    Math.round(Math.random() * 160),
                    Math.round(Math.random() * 160)
                ].join(',') + ')';
            } else {
                return colors[weight % 5];
            }
        }
    } else if (type === 'barHorizontal') {
        option.yAxis.data = tmp1
        option.series[0].data = tmp2
    } else if (type === 'barVertical') {
        option.xAxis.data = tmp1
        option.series[0].data = tmp2
    }
    if (option.legend) {
        option.legend['show'] = showLabel
    }
    if (colors && colors.length > 0 && colorFlag) {
        option['color'] = colors
    } else {
        option['color'] = DEFAULT_COLORS
    }
    return option;
}